package guis.visitant;

import exceptions.RepositoryException;
import guis.RegisterPanel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;

import classes.Classroom;
import classes.Visitant;

import communication.Facade;

public class InsertVisitantWindow extends VisitantWindow
{
	private static final long	serialVersionUID	= 1032307379765488755L;

	public InsertVisitantWindow( JFrame source, RegisterPanel< Visitant > registerPanel, Facade facade )
	{
		super( "Inserir Novo Visitante", "inserir", source, registerPanel, facade );
		this.facade = facade;
		this.registerPanel = registerPanel;
		createActions();
	}

	private void createActions()
	{
		cancelButton.addActionListener( new ActionListener()
		{

			@Override
			public void actionPerformed( ActionEvent arg0 )
			{
				setVisible( false );
			}

		} );

		actionButton.addActionListener( new ActionListener()
		{

			@Override
			public void actionPerformed( ActionEvent arg0 )
			{
				String name = nameTextField.getText();
				int day = ( Integer ) dayComboBox.getSelectedItem();
				int month = ( Integer ) monthComboBox.getSelectedItem();
				int year = ( Integer ) yearComboBox.getSelectedItem();
				Classroom classroom = ( Classroom ) classroomComboBox.getSelectedItem();
				int classroomId = 0;
				if ( classroom != null )
					classroomId = classroom.getId();

				Visitant visitant = new Visitant( name, new utils.Date( day, month, year ), classroomId );

				try
				{
					facade.addVisitant( visitant );
					registerPanel.loadTable( null );
					setVisible( false );
				}
				catch ( RepositoryException e )
				{

				}
			}

		} );
	}
}
